package com.hanrui.android.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.hanrui.android.GlideApp;
import com.hanrui.android.R;
import com.hanrui.android.activity.LoginActivity;
import com.hanrui.android.activity.MainActivity;
import com.hanrui.android.activity.mine.BillHelperActivity;
import com.hanrui.android.library.base.BaseFragment;
import com.hanrui.android.model.User;
import com.hanrui.android.model.VersionBean;
import com.hanrui.android.net.contract.mine.MineContract;
import com.hanrui.android.net.presenter.mine.MinePresenter;
import com.hanrui.android.utils.BaseUtils;
import com.hanrui.android.widget.dialog.UpdateDialog;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 个人中心
 * Created by zhengwei on 2018/1/31.
 */

public class PersonalFragment extends BaseFragment<MinePresenter> implements MineContract.View {

    @BindView(R.id.head)
    ImageView head;
    @BindView(R.id.location)
    TextView location;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.work)
    TextView work;
    @BindView(R.id.left_iv)
    ImageView leftIv;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.change)
    RelativeLayout change;
    @BindView(R.id.left_iv1)
    ImageView leftIv1;
    @BindView(R.id.title1)
    TextView title1;
    @BindView(R.id.bill)
    RelativeLayout bill;
    @BindView(R.id.left_iv2)
    ImageView leftIv2;
    @BindView(R.id.title2)
    TextView title2;
    @BindView(R.id.wages)
    RelativeLayout wages;
    @BindView(R.id.left_iv3)
    ImageView leftIv3;
    @BindView(R.id.version)
    TextView version;
    @BindView(R.id.right_icon)
    ImageView rightIcon;
    @BindView(R.id.update)
    RelativeLayout update;
    @BindView(R.id.login_out)
    TextView loginOut;
    @BindView(R.id.update_logo)
    TextView updateLogo;
    User user;
    MainActivity mActivity;
    UpdateDialog dialog;

    double appVersion,currentVersion;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_personal;
    }

    @Override
    protected void initInjector() {mFragmentComponent.inject(this); }

    @Override
    protected void initView(View view) {
        mActivity = (MainActivity) getActivity();
        GlideApp.with(mActivity)
                .load(R.mipmap.default_avatar2x)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(head);

        currentVersion = Double.valueOf(BaseUtils.getVersionName(getActivity()));
        version.setText("版本号"+currentVersion);

        mPresenter.getVersion(getActivity());
    }

    @Override
    public void onResume() {
        super.onResume();
        init();
    }

    private void init() {
        user = BaseUtils.getUser(getActivity());
        if (null != user) {
            name.setText(user.getUserName());
        }
    }

    public static PersonalFragment newInstance() {
        return new PersonalFragment();
    }




    @OnClick({R.id.login_out,R.id.change,R.id.bill,R.id.wages,R.id.update})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_out:
                mActivity.openActivityAndCloseThis(LoginActivity.class);
                break;
            case R.id.change:
                ToastUtils.showShort("开发中...");
                break;
            case R.id.bill:
                BillHelperActivity.start(getActivity());
                break;
            case R.id.wages:
                ToastUtils.showShort("开发中...");
                break;
            case R.id.update:
                if (appVersion > currentVersion) {
                    initUpdateDialog();
                    return;
                }
                ToastUtils.showShort("已经是最新版本");
                break;

        }
    }

    @Override
    public void getVersion(VersionBean bean) {

        updateLogo.setVisibility(View.INVISIBLE);

        if (null != bean) {


            appVersion = Double.valueOf(bean.getVersion());

            if (appVersion > currentVersion) {

                initUpdateDialog();
                updateLogo.setVisibility(View.VISIBLE);

            }

        }

    }


    //更新dialog
    public void initUpdateDialog(){
        if (dialog == null) {
            dialog = new UpdateDialog(getActivity());
        }
        dialog.show();
        dialog.setCanceledOnTouchOutside(false);
    }
}
